package com.example.yixueguan.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.yixueguan.R;
import com.example.yixueguan.bean.Student;
import com.example.yixueguan.bean.StudentsDAO;

public class XuankeActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText etStudentid;
    private EditText etStudentname;
    private EditText etKehao;
    private EditText etkeming;
    private Button btnAdd;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuanke);
        initView();
    }

    private void initView() {
        etStudentid=(EditText)findViewById(R.id.et_studentid);
        etStudentname = (EditText) findViewById(R.id.et_studentname);
        etKehao= (EditText) findViewById(R.id.et_studentkehao);
        etkeming = (EditText) findViewById(R.id.et_studentkeming);
        btnAdd = (Button) findViewById(R.id.btn_add);
        //设置按钮的点击事件
        btnAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //当单击“添加”按钮时，获取添加信息
        String studentid=etStudentid.getText().toString().trim();
        String studentname = etStudentname.getText().toString().trim();
        String majoy = etKehao.getText().toString().trim();
        String studentclass = etkeming.getText().toString();

        //检验信息是否正确
        if (TextUtils.isEmpty(studentid)) {
            Toast.makeText(this, "请输入学号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(studentname)) {
            Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(majoy)) {
            Toast.makeText(this, "请输入课号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(studentclass)) {
            Toast.makeText(this, "请输入课名", Toast.LENGTH_SHORT).show();
            return;
        }

        //添加学生信息
        Student o =new Student();
        o.studentid= studentid;
        o.studentname = studentname;
        o.majoy = majoy;
        o.studentclass = studentclass;

        //创建数据库访问对象
        StudentsDAO dao = new StudentsDAO(getApplicationContext());
        //打开数据库
        dao.open();
        //执行数据库访问方法
        long result = dao.addStudents(o);

        if (result > 0) {
            Toast.makeText(this, "选课成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "选课失败", Toast.LENGTH_SHORT).show();
        }
        //关闭数据库
        dao.close();
        //关闭活动
        finish();
    }
}
